package com.ws.base.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {

    private final static String FILEPATH = PropertiesUtil.class
            .getResource("/conf.properties").getPath();
    private Properties prop;

    private PropertiesUtil() {
    }

    private static class SingletonHolder {
        private final static PropertiesUtil INSTANCE = new PropertiesUtil();
    }

    public static PropertiesUtil getInstance() {
        PropertiesUtil propertiesUtil = SingletonHolder.INSTANCE;
        propertiesUtil.init();
        return propertiesUtil;
    }

    private void init() {
        File file = new File(FILEPATH);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(inputStream);
            prop = properties;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public String getProperty(String key) {
        return getProperty(key, null);
    }

    public String getProperty(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }

    public int getIntProperty(String key) {
        return Integer.parseInt(prop.getProperty(key));
    }

    public long getLongProperty(String key) {
        return Long.parseLong(prop.getProperty(key));
    }

    public double getDoubleProperty(String key) {
        return Double.parseDouble(prop.getProperty(key));
    }

    public boolean getBooleanProperty(String key) {
        return Boolean.parseBoolean(prop.getProperty(key));
    }

}
